package com.Lists.LinkedLists;

import com.Lists.LinkedLists.common.ListElement;

public class SingleLinkedList extends ListElement {
	
	/**
	 * 
	 * @param data
	 */
	public SingleLinkedList(Object data) {
		super(data);
	}
	/**
	 * 
	 */
	public SingleLinkedList() {
		super();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		SingleLinkedList element = new SingleLinkedList();
		SingleLinkedList deleteme = new SingleLinkedList(new Integer(2));
		element = element.insertInFront(element, new Integer(1));
		element = element.insertInFront(element, new Integer(2));
		element = element.insertInFront(element, new Integer(3));
		element.displayList(element);
		System.out.println("Value To be Removed: " + deleteme.data.toString());
		element = element.deleteElement(element, deleteme);
		element.displayList(element);

	}

	public void displayList(SingleLinkedList element) {
		while (element != null) {
			System.out.println(element.toSring());
			element = (SingleLinkedList) element.next;
		}
	}

	public SingleLinkedList insertInFront(SingleLinkedList list, Object data) {
		SingleLinkedList l = new SingleLinkedList(data);
		l.next = list;
		return l;
	}

	public SingleLinkedList find(SingleLinkedList head, Object data) {
		while (head != null && head.data != data) {
			head = (SingleLinkedList) head.next;
		}
		return head;
	}

	public SingleLinkedList deleteElement(SingleLinkedList head,
			SingleLinkedList deleteMe) {
		SingleLinkedList element = head;
		if (Integer.valueOf(head.data.toString()) == Integer.valueOf(deleteMe.data.toString()) ) {
			head = (SingleLinkedList) head.next; // In this case we delete
													// the current heap
													// reference and we're
													// pointing to the next one.
			return head;
		}
		while (element != null) {
			if (Integer.valueOf(element.data.toString()) == Integer.valueOf(deleteMe.data.toString())) {
				head.next = element.next;
				return head;
			}
			element = (SingleLinkedList) element.next;
		}
		return element;
	}

}
